using PEDEngine;
using UnityEngine;
using System;
using UnityEngine.EventSystems;

public static class PEDUIExtend
{
    private static T GetOrAddComponent<T>(this UIBehaviour component) where T : Component
    {
        var ui = component.gameObject.GetComponent<T>();
        if (ui == null)
        {
            ui = component.gameObject.AddComponent<T>();
        }

        return ui;
    }

    #region 单击

    public static  OnPUGUIClick OnPointClick(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;                                                                             
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.OnClick;
    }

    #endregion

    #region 按下

    public static OnPUGUIMouseDown OnMouseDown(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.OnMouseDown;
    }

    #endregion

    #region 抬起

    public static OnPUGUIMouseUp OnMouseUp(this UIBehaviour component,bool isOutLongPress=true)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        listener.IsOutLongPress = isOutLongPress;
        return listener.OnMouseUp;
    }

    #endregion

    #region 开启拖拽   也可以用于常规拖拽

    /// <summary>
    /// 开启可以拖拽事件
    /// </summary>
    /// <param name="component"></param>
    /// <param name="tags"></param>
    /// <param name="onCollision">第一个是拖拽到哪里的目标gameobject，第二个是拖拽的目标本身 </param>
    /// <param name="reset">是否还原到初始位置</param>
    public static void EnableCollisionDrag(this UIBehaviour component, Action<GameObject, GameObject> onCollision,
        bool reset = true)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        listener.EnableCollisionDrag(onCollision, reset);
    }

    /// <summary>
    /// 关闭可以拖拽事件
    /// </summary>
    /// <param name="component"></param>
    public static void DisEnableCollisionDrag(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        listener.DisEnableCollisionDrag();
    }

    #endregion

    #region 长按

    public static OnPUGUIMouseLongPress OnLongMousePress(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.OnLongMousePress;
    }

    #endregion

    #region 开始拖拽

    public static OnPUGUIBeginDrag OnBeginDrag(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.onBeginDrag;
    }

    #endregion

    #region 拖拽中

    public static OnPUGUIDrag OnDrag(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.onDrag;
    }

    #endregion

    #region 结束拖拽

    public static OnPUGUIEndDrag OnEndDrag(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.onEndDrag;
    }

    #endregion

    #region 移入事件

    public static OnPUGUIEnter OnEnter(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.OnEnter;
    }

    #endregion
    
    #region 移出事件

    public static OnPUGUIExit OnExit(this UIBehaviour component)
    {
        if (component == null || component.gameObject == null)
        {
            Debug.LogError("component is null");
            return null;
        }

        PUGUIListener listener = component.GetOrAddComponent<PUGUIListener>();
        return listener.OnExit;
    }
    
    #endregion
    
}